package com.wafersystems.cloud.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wafersystems.cloud.bo.abstractbo.AbstractSwitchDevice;
import com.wafersystems.cloud.dto.DeviceCtrlCommand;
import com.wafersystems.cloud.service.impl.DeviceService;
import com.wafersystems.cloud.vo.ReturnValue;

import lombok.extern.slf4j.Slf4j;

/**
 * 微服务信息查询controller.
 * 
 * @author wafer
 *
 */
@RestController
@RequestMapping(value = "/smarthome/v2.0/api")
@Slf4j
public class CustomController {

  @Autowired
  private DeviceService deviceService;


  /**
   * 设备控制接口.
   * 
   * @param command
   * @return
   */
  @RequestMapping(value = "/ctrl/device", method = RequestMethod.POST)
  public Object ctrlSocket(@RequestBody DeviceCtrlCommand cmd) throws Exception {
    log.info("device control {}", cmd);
    AbstractSwitchDevice device = deviceService.getDoDevice(cmd.getDevId());
    if (null == device) {
      return new ReturnValue(ReturnValue.fail, "", "device not found");
    }
    switch (cmd.getApply()) {
      case 1: {
        device.open();
        break;
      }
      case 0: {
        device.close();
        break;
      }
    }
    return new ReturnValue(ReturnValue.success, "", "execute success");
  }


}
